<template>
  <div>
    <el-form :model="dataForm" ref="queryForm" :inline="true">
      <el-form-item label="标题" prop="noticeTitle">
        <el-input
          v-model="dataForm.noticeTitle"
          placeholder="请输入关键字"
          clearable
        />
      </el-form-item>
      <el-form-item label="操作人员" prop="createBy">
        <el-input
          v-model="dataForm.createBy"
          placeholder="请输入操作人员"
          clearable
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="日志类型" clearable>
          <el-option
            v-for="option in noticeTypeOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getNoticeList">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="dataListLoading" :data="noticeList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="id" width="80"/>
      <el-table-column
        label="标题"
        align="center"
        prop="title"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="简介"
                       prop="content"
                       header-align="center"
                       align="center" width="520"
                       text-overflow: ellipsis
                       :show-overflow-tooltip="true">
        <!--<template slot-scope="scope">-->
        <!--  <div v-html="scope.row.content"></div>-->
        <!--</template>-->
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type" width="80">
        <template slot-scope="scope" >
          <el-tag size="small" type="warning" v-if="scope.row.type === 3">日志</el-tag>
          <el-tag size="small" type="success" v-else>公告</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope" v-show="scope.row.status === 0">
          <el-tag size="small" type="success" v-if="scope.row.status === 1">DONE</el-tag>
          <el-tag size="small" type="danger" v-else>TODO</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" width="100"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="addOrUpdateHandle(scope.row.id)"
          >修改
          </el-button>
          <el-button
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-show="totalCount > 0"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNum"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper"/>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getNoticeList"></add-or-update>
  </div>
</template>
<script>
import AddOrUpdate from './notice-add-or-update'

export default {
  components: {
    AddOrUpdate
  },
  name: 'notice',
  dict: ['sys_notice_status', 'sys_notice_type'],
  data () {
    return {
      dataForm: {
        // 查询参数
        noticeTitle: undefined,
        createBy: undefined,
        type: undefined,
        status: undefined
      },
      // 公告类型
      noticeTypeOptions: [
        {
          value: undefined,
          label: '所有'
        },
        {
          value: 1,
          label: '通知'
        },
        {
          value: 2,
          label: '公告'
        }, {
          value: 3,
          label: '日志'
        }],
      // 遮罩层
      dataListLoading: true,
      // 选中数组
      // ids: [],
      // 非单个禁用
      // single: true,
      // 非多个禁用
      // multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      totalCount: 0,
      // 公告表格数据
      noticeList: [],
      // 是否弹出编辑框
      addOrUpdateVisible: false,
      // 选择的数据（批量删除）
      dataListSelections: [],
      // 分页
      pageNum: 1,
      pageSize: 10,
    }
  },
  created () {
    this.getNoticeList()
  },
  methods: {
    // 获取公告列表
    getNoticeList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/notice/search'),
        method: 'post',
        data: this.$http.adornData({
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          noticeTitle: this.dataForm.noticeTitle,
          createBy: this.dataForm.createBy,
          type: this.dataForm.type,
          status: this.dataForm.status
        })
      }).then(({data}) => {
        if (data && data.code === 200) {
          this.noticeList = data.page.data
          this.totalCount = data.page.total
        } else {
          this.noticeList = []
          this.totalCount = 0
          this.$message.error(data.msg)
        }
        this.dataListLoading = false
      })
    },
    // 获取具体要修改得公告
    getNoticeById (id) {
      let noticeInfo = {}
      if (id !== undefined) {
        this.noticeList.map(item => {
          if (item.id === id) {
            noticeInfo = item
          }
        })
      }
      return noticeInfo
    },
    // 添加or更新
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        // 为子组件addOrUpdate传递部门数据
        this.$refs.addOrUpdate.dataFromNotice = this.getNoticeById(id)
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      // this.ids = selection.map(item => item.noticeId)
      // this.single = selection.length !== 1
      // this.multiple = !selection.length
      this.dataListSelections = selection
    },
    // 删除
    deleteHandle (row) {
      let ids
      let titleNames
      if (this.dataListSelections.length >= 1) {
        ids = this.dataListSelections.map(item => {
          return item.id
        })
        titleNames = this.dataListSelections.map(item => {
          return item.title
        })
      } else {
        ids = [row.id]
        titleNames = [row.title]
      }
      this.$confirm(`确定对公告标题为[${titleNames.join(',')}]进行[${titleNames.length === 1 ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/notice/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getNoticeList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageNum = 1
      this.getNoticeList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageNum = val
      this.getNoticeList()
    },
    // 重置按钮操作
    resetQuery () {
      this.dataForm.noticeTitle = ''
      this.dataForm.createBy = ''
      this.dataForm.status = undefined
    },
  },
}
</script>

<style lang="css">
.el-tooltip__popper{
  display: none }
</style>
